package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllCrawlerpictures", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture"),
		@NamedQuery(name = "findCrawlerpictureByName", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.name = ?1"),
		@NamedQuery(name = "findCrawlerpictureByNameContaining", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerpictureByPath", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.path = ?1"),
		@NamedQuery(name = "findCrawlerpictureByPathContaining", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.path like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerpictureByDescription", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.description = ?1"),
		@NamedQuery(name = "findCrawlerpictureByDescriptionContaining", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllCrawlerpicturesByCrawler", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.crawler.id = ?1"),
		@NamedQuery(name = "findCrawlerpictureById", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.id = ?1"),
		@NamedQuery(name = "findCrawlerpictureByPrimaryKey", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.id = ?1 "),
		@NamedQuery(name = "deleteCrawlerpicturesByIds", query = "delete Crawlerpicture where id in (?1)"),
		@NamedQuery(name = "findCrawlerpicturesByIds", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where myCrawlerpicture.id in (?1)"),
		@NamedQuery(name = "findCrawlerpictureByPhyscialPrimaryKey", query = "select myCrawlerpicture from Crawlerpicture myCrawlerpicture where  myCrawlerpicture.path = ?1 and myCrawlerpicture.crawler.id = ?2"),
		@NamedQuery(name = "countCrawlerpicturesByName", query = "select count(myCrawlerpicture)  from Crawlerpicture myCrawlerpicture where myCrawlerpicture.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "crawlerpicture")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Crawlerpicture")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Crawlerpicture implements Serializable, Comparable<Crawlerpicture> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "path")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String path;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "crawler", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Crawler crawler;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPath() {
		return this.path;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setCrawler(Crawler crawler) {
		this.crawler = crawler;
	}

	public Crawler getCrawler() {
		return crawler;
	}

	public void copy(Crawlerpicture that) {

		setId(that.getId());

		setName(that.getName());
		setPath(that.getPath());
		setDescription(that.getDescription());

		setCrawler(that.getCrawler());

	}

	public Crawlerpicture() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("path=[").append(path).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Crawlerpicture))
			return false;
		Crawlerpicture comparedCrawlerpicture = (Crawlerpicture) obj;
		if (!CheckPropertyEqual(this.path, comparedCrawlerpicture.path)) {
			return false;
		}
		if (!CheckPropertyEqual(this.crawler.id, comparedCrawlerpicture.crawler.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Crawlerpicture o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
